package com.szrt.cll.test.core.base;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public abstract class BaseController<T> {
	
	@Autowired
	public abstract BaseService<T> getBaseService();
	
	@ResponseBody
	@RequestMapping("/add")
	public Map<String,String> add(T entity,HttpServletRequest request){
		Map<String,String> map  = new HashMap<String,String>();
		int counter = getBaseService().add(entity);
		if(counter ==1){
			map.put("msg", "增加成功...");
			map.put("result", "success");
		}else{
			map.put("msg", "增加失败...");
			map.put("result", "failure");
		}
		
		return map;
		
	}
	
	@ResponseBody
	@RequestMapping("/deleteByIds")
	public Map<String,String> deleteByIds(@RequestBody List<Integer> ids){
		Map<String,String> map  = new HashMap<String,String>();
		int counter = getBaseService().deleteByIds(ids);
		if(counter > 0){
			map.put("msg", "删除成功...");
			map.put("result", "success");
		}else{
			map.put("msg", "删除失败...");
			map.put("result", "failure");
		}
		
		return map;
		
	}
	
	
	@ResponseBody
	@RequestMapping("/findById")
	public T findById(Integer id){
		T entity = getBaseService().findById(id);
		return entity;
	}
	
	@ResponseBody
	@RequestMapping("/update")
	public Map<String,String> update(T entity){
		
		Map<String,String> map  = new HashMap<String,String>();
		int counter = getBaseService().update(entity);
		if(counter ==1){
			map.put("msg", "修改成功...");
			map.put("result", "success");
		}else{
			map.put("msg", "修改失败...");
			map.put("result", "failure");
		}
		
		return map;
		
	}
	
	@ResponseBody
	@RequestMapping("/findAll")
	public Map<String,Object> findAll(Integer page, Integer rows){
		System.err.println("进入findAll方法....");
		if(null == page){
			page=1;
		}
		
		Map<String,Object> pageMap = new HashMap<String,Object>();
		
		List<T> list= getBaseService().findAll(pageMap);
		Map<String,Object> map=new HashMap<>();
		
		
		map.put("rows",list);
		map.put("result","success");
		
		return map;
	}
}
